#include "../engine/core.h"
#include "./BehaviorTree/IBTNode.h"
#include "./BehaviorTree/BehaviorTree.h"

//----------------------------------------------------------------------------------------------------------------------------
INT IBTNode::mNodeCount = 0;

//----------------------------------------------------------------------------------------------------------------------------
IBTNode::IBTNode()
: mpBehaviorTree( NULL )
, mParent( NULL )
{
	mIndexInParent = mNodeCount;
	mNodeCount++;

	mChilds.clear();
	mName = "IBTNode";
	mId = -1;
	mNodeStatus = STATUS_NONE;
}

//----------------------------------------------------------------------------------------------------------------------------
IBTNode::IBTNode( IBTNode * parent )
: mpBehaviorTree( NULL )
, mParent( parent )
{
	mIndexInParent = mNodeCount;
	mNodeCount++;

	mChilds.clear();
	mName = "IBTNode";
	mId = -1;
	mNodeStatus = STATUS_NONE;
}

//----------------------------------------------------------------------------------------------------------------------------
IBTNode::~IBTNode()
{
	itemBTNode index = mChilds.begin();
	for( ; index != mChilds.end(); index++ )
	{
		IBTNode* node = (*index);
		delete node;
		node = NULL;
	}
	mChilds.clear();
}

VOID IBTNode::pushNode( IBTNode* node )
{
	mChilds.push_back( node );
}

IBTNode*	IBTNode::searchNode( INT id )
{
	itemBTNode index = mChilds.begin();
	for( ; index != mChilds.end(); index++ )
	{
		IBTNode* child = (*index);
		if ( id == child->getId() )
		{
			return child;
		}

		IBTNode* child_child = child->searchNode( id );
		if ( NULL != child_child )
		{
			return child_child;
		}
	}

	return NULL;
}

VOID IBTNode::setCurrentExcute()
{
	mpBehaviorTree->setCurrentExcuteID(getId());
}

VOID IBTNode::unsetCurrentExcute()
{
	mpBehaviorTree->setCurrentExcuteID( 0 );
}
